package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.dto.OrderFormDto;
import com.itheima.mapper.OrderDetailsMapper;
import com.itheima.mapper.OrderFormMapper;
import com.itheima.pojo.*;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrderFormService;
import com.itheima.service.ShoppingCarService;
import com.itheima.service.UserService;
import com.itheima.vo.AddressBookVo;
import com.itheima.vo.OrderFormVo;
import com.itheima.vo.PagingQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author :WuJunJie
 * packageName :com.itheima.service.impl
 * fileName :OrderFormServiceImpl
 * creationTime :2022-11-25 16:15
 */
@Service
public class OrderFormServiceImpl implements OrderFormService {

    @Autowired
    private OrderFormMapper orderFormMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private ShoppingCarService shoppingCarService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Override
    public PagingQueryVo<OrderForm> selectPage(LocalDateTime beginTime, LocalDateTime endTime, String number, Integer page, Integer pageSize) {
        Integer start=(page-1)*pageSize;
        List<OrderForm> orderForms=orderFormMapper.selectPage( beginTime,  endTime, number, start, pageSize);
        PagingQueryVo<OrderForm> pagingQueryVo = new PagingQueryVo<>();
        pagingQueryVo.setRecords(orderForms);
        pagingQueryVo.setTotal(orderFormMapper.countTotal(beginTime,  endTime, number));
        pagingQueryVo.setSize(pageSize);
        pagingQueryVo.setCurrent(page);
        return pagingQueryVo;
    }

    @Override
    public Boolean changeStatus(OrderForm orderForm) {
        if (orderForm.getStatus()==4){
            orderForm.setCheckoutTime(LocalDateTime.now());
        }
        Integer change = orderFormMapper.changeStatus(orderForm);
        return change==1;
    }
    @Transactional
    @Override
    public Boolean insertOrder(OrderFormDto orderFormDto) {
        OrderForm orderForm = new OrderForm();
        BeanUtils.copyProperties(orderFormDto,orderForm);
        orderForm.setUserId(ThreadLocalUtil.getid());
        orderForm.setId(IdUtil.getSnowflakeNextIdStr());
        orderForm.setNumber(orderForm.getId());
        orderForm.setCreateTime(LocalDateTime.now());
        orderForm.setCreateUser(ThreadLocalUtil.getid());
        User user=userService.selectById(ThreadLocalUtil.getid());
        orderForm.setPhone(user.getPhone());
        orderForm.setUserName(user.getName());
        AddressBookVo addressBookVo = addressBookService.selectAddressBookById(orderForm.getAddressBookId());
        orderForm.setAddress(addressBookVo.getDetail());
        orderForm.setConsignee(addressBookVo.getConsignee());
        Integer amount=0;
        List<ShoppingCar> shoppingCars=shoppingCarService.selectListByUserId();
        List<OrderDetails> orderDetailsList = new ArrayList<>();
        for (ShoppingCar shoppingCar : shoppingCars) {
            OrderDetails orderDetails = new OrderDetails();
            orderDetails.setName(shoppingCar.getName());
            orderDetails.setNumber(shoppingCar.getNumber().toString());
            amount += shoppingCar.getAmount();
            orderDetails.setId(IdUtil.getSnowflakeNextIdStr());
            orderDetails.setOrderId(orderForm.getId());
            orderDetails.setCreateTime(LocalDateTime.now());
            orderDetails.setUpdateTime(LocalDateTime.now());
            orderDetails.setCreateUser(ThreadLocalUtil.getid());
            orderDetails.setUpdateUser(ThreadLocalUtil.getid());
            orderDetailsList.add(orderDetails);
        }
        orderForm.setAmount(amount);
        Integer insertorderDetails=orderDetailsMapper.insertListOrderDetails(orderDetailsList);
        Integer insert=orderFormMapper.insertOrderForm(orderForm);
        if (insert>0&&insertorderDetails>0){
            Boolean aBoolean = shoppingCarService.deleteAllShoppingCar();
            return aBoolean;
        }
        return false;
    }

    @Override
    public PagingQueryVo<OrderFormVo> userSelectPage(Integer page, Integer pageSize) {
        Integer start=(page-1)*pageSize;
        List<OrderForm> orderForms = orderFormMapper.userSelectPage(start, pageSize,ThreadLocalUtil.getid());
        List<OrderFormVo> orderFormVos = BeanUtil.copyToList(orderForms, OrderFormVo.class);
        Set<String> ids = orderForms.stream().map(OrderForm::getId).collect(Collectors.toSet());
        List<OrderDetails> orderDetails=orderDetailsMapper.selectByOrderIds(ids);
        Map<String, List<OrderDetails>> orderDetailsMap = orderDetails.stream().collect(Collectors.groupingBy(OrderDetails::getOrderId));
        for (OrderFormVo orderFormVo : orderFormVos) {
            orderFormVo.setOrderDetails(orderDetailsMap.get(orderFormVo.getId()));
        }
        PagingQueryVo<OrderFormVo> pagingQueryVo = new PagingQueryVo<>();
        pagingQueryVo.setCurrent(page);
        pagingQueryVo.setSize(pageSize);
        Integer count = orderFormMapper.userCounttotal(start, pageSize);
        pagingQueryVo.setTotal(count);
        pagingQueryVo.setPages(count/pageSize);
        pagingQueryVo.setRecords(orderFormVos);
        return pagingQueryVo;
    }
}
